﻿/// <reference path="../Pages/Content/Help.js" />


//-----------------------------------------------------------
// Description: Xử lý show popup thông tin Help
//-----------------------------------------------------------
ShowInfoHelp = new function () {
    this.helpID = 0;
    this.service = -1;
    this.typeShow = 0; //0:Thêm mới, 1:Sửa

    this.m_idShowInfoHelp = "divShowInfoHelp";

    this.showPopup = function (_helpID, _typeShow, _service) {
        this.helpID = _helpID;
        this.service = _service;
        this.typeShow = _typeShow;

        if ($("#" + this.m_idShowInfoHelp).length == 0) {
            $("body").append("<div id='" + this.m_idShowInfoHelp + "'></div>");
            $("#" + ShowInfoHelp.m_idShowInfoHelp).setTemplateURL("/Template/Popup/ShowInfoHelp.htm");
            $("#" + ShowInfoHelp.m_idShowInfoHelp).processTemplate(null);
            var top = (($(window).height() / 2) - ($("#divPopup_ShowInfoHelp").outerHeight() / 2));
            var left = (($(window).width() / 2) - ($("#divPopup_ShowInfoHelp").outerWidth() / 2));
            if (top < 0) top = 0;
            if (left < 0) left = 0;
            // IE6 fix
            if ($.browser.msie && parseInt($.browser.version) <= 6) top = top + $(window).scrollTop();

            $("#divPopup_ShowInfoHelp").css({
                top: top + 'px',
                left: left + 'px'
            });
            this.bindInfoHelp();
        }

        try {
            $("#divPopup_ShowInfoHelp").draggable({ handle: $("#divPopup_ShowInfoHelp .barpopup") });
            $("#divPopup_ShowInfoHelp .barpopup").css({ cursor: 'move' });
        } catch (e) { /* requires jQuery UI draggables */ }


    };

    this.bindInfoHelp = function () {
        if (ShowInfoHelp.typeShow == 0) {
            $("#txtHelpName").val("");
            $("#divHelpOrder").hide();
            $(".optHelpStatus").remove();
            for (var i = 1; i > -1; i--) {
                $("#ddlHelpStatus").append('<option value="' + i + '" class="optHelpStatus">' + ShowInfoHelp.convertStatus(i) + '</option>');
            }
            ShowInfoHelp.bindHelpParent(ShowInfoHelp.helpID);
        }
        else {
            $.ajax({
                type: "GET",
                url: "/Handler/HandlerContent.ashx",
                data: { id: ShowInfoHelp.helpID, t: 'GetInfoHelpCategory' },
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function (data) {
                    $("#txtHelpName").val(data.Items[0].Name);
                    $("#divHelpOrder").show();
                    $("#txtHelpOrder").val(data.Items[0].Order);
                    ShowInfoHelp.bindHelpParent(data.Items[0].ParentID);
                    $(".optHelpStatus").remove();
                    for (var i = 0; i < 2; i++) {
                        if (i == data.Items[0].Status)
                            $("#ddlHelpStatus").append('<option value="' + i + '" selected="selected" class="optHelpStatus">' + ShowInfoHelp.convertStatus(i) + '</option>');
                        else
                            $("#ddlHelpStatus").append('<option value="' + i + '" class="optHelpStatus">' + ShowInfoHelp.convertStatus(i) + '</option>');
                    }


                }
            });
        }
    };

    this.bindHelpParent = function (_parentID) {
        $(".optHelpParrent").remove();
        if (_parentID == -1) {
            $("#ddlHelpParrent").append('<option value="' + "-1" + '" selected="selected" class="optHelpParrent" >' + "Danh mục gốc" + '</option>');
        }
        else {
            $("#ddlHelpParrent").append('<option value="' + "-1" + '" class="optHelpParrent" >' + "Danh mục gốc" + '</option>');
        }
        $.ajax({
            type: "GET",
            url: "/Handler/HandlerContent.ashx",
            data: { t: 'GetListHelpCategoryAll', parrentID: -1, level: 1, type: 0, status: -1, service: ShowInfoHelp.service },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                for (var i = 0; i < data.Items.length; i++) {
                    if (data.Items[i].HelpCategoryID == _parentID) {
                        $("#ddlHelpParrent").append('<option value="' + data.Items[i].HelpCategoryID + '" selected="selected" class="optHelpParrent" >' + data.Items[i].Name + '</option>');
                    }
                    else {
                        $("#ddlHelpParrent").append('<option value="' + data.Items[i].HelpCategoryID + '" class="optHelpParrent" >' + data.Items[i].Name + '</option>');
                    }
                }
            }
        });
    };

    this.close = function () {
        if ($("#" + this.m_idShowInfoHelp).length != 0) {
            $("#" + this.m_idShowInfoHelp).remove();
        }
    };

    this.updateInfoHelp = function () {
        if ($("#txtHelpName").val() == '') {
            jAlert(MESSAGE.errorInfo, MESSAGE.title);
            return;
        }
        var m_order = 0;
        if (ShowInfoHelp.typeShow == 1) {
            m_order = $("#txtHelpOrder").val();
            if ((!Untils.isInteger(m_order)) || (Untils.isEmpty(m_order))) {
                jAlert('Thứ tự không được rỗng và phải là số nguyên!', MESSAGE.title, function () {
                    $("#txtHelpOrder").focus();
                });
                return;
            }
            m_order = parseInt(m_order);
        }
        if (ShowInfoHelp.typeShow == 0) {
            var _data =
                {
                    name: $("#txtHelpName").val(),
                    parentID: $("#ddlHelpParrent").val(),
                    status: $("#ddlHelpStatus").val(),
                    service: ShowInfoHelp.service
                };
            $.ajax({
                type: "POST",
                url: "/WebService/WSContent.asmx/AddHelp",
                data: JSON.encode(_data),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function (data) {
                    if (data.d > 0) {
                        jAlert(MESSAGE.addSuccess, MESSAGE.title);
                        Pages_admin_Help.loadHelpCategoryAll();
                        ShowInfoHelp.close();
                    }
                    else {
                        if (data.d == -1) {
                            jAlert(MESSAGE.notPermission, MESSAGE.title);
                        }
                        else {
                            jAlert(MESSAGE.error, MESSAGE.title);
                        }
                    }
                }
            });
        }
        else {
            var _data =
                {
                    helpCategoryID: ShowInfoHelp.helpID,
                    name: $("#txtHelpName").val(),
                    parentID: $("#ddlHelpParrent").val(),
                    order: m_order,
                    status: $("#ddlHelpStatus").val(),
                    service: ShowInfoHelp.service
                };
            $.ajax({
                type: "POST",
                url: "/WebService/WSContent.asmx/UpdateHelpInfo",
                data: JSON.encode(_data),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function (data) {
                    if (data.d > 0) {
                        jAlert(MESSAGE.updateSuccess, MESSAGE.title);
                        Pages_admin_Help.loadHelpCategoryAll();
                        ShowInfoHelp.close();
                    }
                    else {
                        if (data.d == -1) {
                            jAlert(MESSAGE.notPermission, MESSAGE.title);
                        }
                        else {
                            jAlert(MESSAGE.error, MESSAGE.title);
                        }
                    }
                }
            });
        }
    };

    this.convertStatus = function (_status) {
        if (_status == 0)
            return MESSAGE.status_lock;
        if (_status == 1)
            return MESSAGE.status_active;
    }

}